package com.open.basic.tree.test;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: system
 * @Date: 2024/4/24 22:16
 * @Version: V1.0
 * @description: desc:
 */

public class TreeUtil {

    public static List<CheckCatalog> getTree(List<CheckCatalog> listNode) {
        List<CheckCatalog> resultList = new ArrayList<>();
        listNode.stream().filter(node -> node.getCheckUpperId().equals("0")).collect(Collectors.toList())
                .forEach(r -> {
                    CheckCatalog areaTreeResult = TreeUtil.packageTree(listNode, r);
                    resultList.add(areaTreeResult);
                });
        resultList.get(0).setDisabled(true);
        return resultList;
    }

    public static CheckCatalog packageTree(List<CheckCatalog> checkCatalogList, CheckCatalog rootCheckCatalog) {
        if (checkCatalogList != null && !checkCatalogList.isEmpty()) {
            checkCatalogList.forEach(CheckCatalog -> judgeObj(CheckCatalog, rootCheckCatalog));
            if (rootCheckCatalog.getList() != null && !rootCheckCatalog.getList().isEmpty()) {
                rootCheckCatalog.getList().forEach(r -> packageTree(checkCatalogList, r));
            }
        }
        return rootCheckCatalog;
    }

    public static void judgeObj(CheckCatalog CheckCatalog, CheckCatalog rootCheckCatalog) {
        List<CheckCatalog> children = rootCheckCatalog.getList();
        if (children == null) {
            children = new ArrayList<>();
        }
        if (CheckCatalog != null && !CheckCatalog.getCheckUpperId().isEmpty() && rootCheckCatalog.getCheckId().equalsIgnoreCase(CheckCatalog.getCheckUpperId())) {
            children.add(CheckCatalog);
            rootCheckCatalog.setList(children);
        }
    }

    public static void main(String[] args) {
        //封装数据
        List<CheckCatalog> listNode = new ArrayList<>();
        listNode.add(new CheckCatalog("1", "0", "parent", false));
        listNode.add(new CheckCatalog("88", "0", "parent", false));

        listNode.add(new CheckCatalog("2", "1", "second1", false));
        listNode.add(new CheckCatalog("3", "2", "second2", false));

        listNode.add(new CheckCatalog("4", "2", "three1", false));
        listNode.add(new CheckCatalog("5", "2", "three2", false));

        listNode.add(new CheckCatalog("6", "3", "three3", false));
        listNode.add(new CheckCatalog("7", "3", "three4", false));


        listNode.add(new CheckCatalog("8", "6", "four1", false));
        listNode.add(new CheckCatalog("9", "6", "four2", false));

        listNode.add(new CheckCatalog("10", "9", "five1", false));
//转换为tree


        List<CheckCatalog> tree = getTree(listNode);
        System.out.println(tree);
    }
}
